package com.guli.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.guli.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000118636395";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCm04M/nFrVx6XE7NM228IoZ/5GvCzrklnLOnLKt71hYYKZDczYnppKpOWKZLPme1JFIcqBaRzSvLNIRvv+Qg7Xqjj5QKb+RQPeOPtcH30DBxnYJTyolIGrk3+x51T3GqT9KrmU7VFjGFrQOWIPGP5LLEoIQAJL5s5Kx7qkvsYksJ3kSBiy1neSEQJY1w/Tp87u3oe63PZqB8ry+GyD1MTxOBuspcptE4VKPXe1ZEch5DqY6Vf5HzKDzzVs+q31YduqTb1U+NCvkHf87eTkTWVdDQwSZPu2y7t5RVoNDlzyDTmXkS4Yzxzng4Ht4JQD3ZNl8n8lDIyDcf+3nS1jO5H1AgMBAAECggEAMcKNN4APqAxw6ZgqOAa8lwjN05N1Yp64oeHBM64yksVfCwAOxd4o73LX9Lus3WaTV4OIv6j2q/UVFM33hBN4EeP4MoCSTu2PBc3BS+0ogKqxjsMsoKwLMX5BbHMv6LTN2aXQzRcsW358N/elxBMUZp/DRUqyZKlnhu6kQVeWCoMYfN0+4tyFp8tq8W6byXw7pMn/+tx56cORiKxaKrTupQ/FckGpeHACroip4VDjwv2L/C2NAjPX4Ctqoxz65SEx3wpE8QbG6Ch2IKTN2UlftbCYBQV3ir9FSULVy1SPxk8KiU98f4wZDkDfNWve/b9dz3jODNInWDhZIxQKr4aPUQKBgQDVWIGEEKuI2RVvbKjIFi0JfiCM5V8yQFzIb5XWAy4Mg/0SjD2WBKztQe5VnM1MrEkCsRfPDDelBloEuuK1SSdr58cRq1M1+4Z9S4uAUyQt/pGVJmfJ0uu4NzUGDIRIPNWQXsm8WkZ7u6E5uh16nnVz5LMHoLzfkP6BgwzUH+RmgwKBgQDILgnK2XDA4e7PDFL8QPZ8t9ueeEtc4oLkmU8rLiiP9dDs6uHlYqvoxms0nTCAWqnzoHOYVFn79626YTfLYq4c/YIqcN3QJhZCccB+eDyIa0/2qjpRygNRq3VjlYpoK5FvmEz/39eQbh/QXsgElBougHr0e8UeOOlWxI08JK38JwKBgQCzqaL4KmmqBtYsTV8dkL9AnTygKpEualE4Ye/WmDrxQz2dnwhGC43tB3bEFhY1mWiqoaQhMnKxwFWSYieE7yQJlNhpotOiRD0PysyhtmJjcVv8NKl2pu7m5Wb7vxKvGrvmpkxRF9z/WhT93iPxHPxgKpoxtEUaKKqLOmdVQ74+LQKBgQCJJ3oQITFvBp4gAJqvOTdpNnPkPRmtt4jlZeFnfYXPNSaD3V8PUq7km2aCsxyzJOuDADGTcmABEq9WnxuiX6daN5vCxnhEEIR53AR+4pTfB1/Paszwkw/5GhghNimRvCA2rMw9GLpSvBFSLE6xTGQv7lnQPylMzsOZbqwyRBNtSwKBgHTrjB0+wAbbv0d1wG6apj+IQ0JKRGt9DG/BEL1SeKml/6oscHoK+CP+9Xp4WP668MhJJjrm/nwFh+RQkPn8fvdBaCRiZgsxbXi99v9x23FOTLyoMtnzuxEMsiKTZxobfGnhwquHBtKH9WQWIKI7Do8llrIbm9fGKNuy0Va1NVky";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1rwcR/Tk84lBPds6K1wSoGVTYYfb6ZY5amC/9sghnuse/Ygm7kfZBntbbqyPqOEIl+qJ4ZpjwiIOmM0M1qb7ujn/ifLM8XA0o5Ny3GIDcDrAUpJ6WFEGxUSnv5ijijtOHU275XTFb7Ju4ngMZvFVBmu9v52UZZZmmPBFSHdk9b7ISNAUe4kro+xzknoapqZAzFk0Ar+L7JTfc4CUeNlh9/8wDaJ8ZDGTaK2/Vhks1Y3sk2QUrByDq3uAbNsShHJKMAMfqBC7LWgTVzDBsIa+wFUcYtx01HcH1aC1Klc0EHHP8oSxGY5ZZ9VGF9DNs6ocxjfYI4lYR/Z76OvbaJVNPwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://c37z155164.wicp.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    private String timeout = "30m";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();
        //timeout_express 支付超时时间-----支付宝自动收单
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;
    }

    /**
     * 支付宝自动收单功能
     * @param orderSn
     * @return
     * @throws UnsupportedEncodingException
     * @throws AlipayApiException
     */
    public String receipt(String orderSn) throws UnsupportedEncodingException, AlipayApiException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, app_id, merchant_private_key, "json", charset, alipay_public_key, sign_type);

        //设置请求参数
        AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();
        //商户订单号，商户网站订单系统中唯一订单号
        String out_trade_no = new String(orderSn.getBytes("ISO-8859-1"), "UTF-8");
        //支付宝交易号
//        String trade_no = new String(alipay_trade_no.getBytes("ISO-8859-1"),"UTF-8");
        //请二选一设置
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"trade_no\":\"\"}");

        //请求
        String result = alipayClient.execute(alipayRequest).getBody();

        //输出
        return result;
    }


}
